package com.wash.shoes.util;

/**
 * @author lijianguo
 * @Date 22/09/2024 请输入类的简介
 **/

import static com.ruoyi.framework.datasource.DynamicDataSourceContextHolder.log;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.wash.shoes.domain.dto.MtUserAddress;
import com.wash.shoes.entity.FactoryPo;
import com.wash.shoes.entity.OrderGoodPo;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.helper.FactoryAddressHelper;

public class AddressCuttingUtil {

    public static List<String> addressCutting(String address) {
        if (address.startsWith("北京市") || address.startsWith("天津市") || address.startsWith("上海市")
            || address.startsWith("重庆市")) {
            address = address.substring(0, 3) + "市辖区" + address.substring(3);
        }
        String regex =
            "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province = null, city = null, county = null, town = null, village = null;
        while (m.find()) {
            province = m.group("province");

            if (province.equals("北京市") || province.equals("天津市") || province.equals("上海市") || province.equals("重庆市")) {
                city = province;

                county = m.group("city");
                if (county.split("区").length > 1) {
                    town = county.substring(county.indexOf("区") + 1);
                    county = county.substring(0, county.indexOf("区") + 1);
                    if (town.contains("区")) {
                        town = town.substring(county.indexOf("区") + 1);
                    }
                } else {
                    county = m.group("county");
                    if (county != null && county.split("区").length > 1) {
                        town = county.substring(county.indexOf("区") + 1);
                        county = county.substring(0, county.indexOf("区") + 1);
                    }
                }
            } else {
                city = m.group("city");

                county = m.group("county");
                if (county != null && !"".equals(county)) {
                    if (county.split("市").length > 1 && county.indexOf("市") < 5) {
                        town = county;
                        county = county.substring(0, county.indexOf("市") + 1);
                        town = town.substring(county.indexOf("市") + 1);
                    }
                    if (county.split("旗").length > 1) {
                        town = county;
                        county = county.substring(0, county.indexOf("旗") + 1);
                        town = town.substring(county.indexOf("旗") + 1);
                    }
                    if (county.split("海域").length > 1) {
                        town = county;
                        county = county.substring(0, county.indexOf("海域") + 2);
                        town = town.substring(county.indexOf("海域") + 2);
                    }
                    if (county.split("区").length > 1) {
                        town = county;
                        county = county.substring(0, county.indexOf("区") + 1);
                        town = town.substring(county.indexOf("区") + 1);
                    }
                }

            }

            if (province != null && !"".equals(province)) {
                province = province + "-";
            }

            if (city != null && !"".equals(city)) {
                city = city + "-";
            }

            if (county != null && !"".equals(county)) {
                county = county + "-";
            }

            if (m.group("town") != null) {
                town += m.group("town");
            }

            if ((county == null || "".equals(county)) && town != null && !"".equals(town)) {
                town = town + "-";
            }
            village = m.group("village");

        }

        String newMachineAdress = province + city + county + town + village;
        if (newMachineAdress != null && !"".equals(newMachineAdress)) {
            newMachineAdress = newMachineAdress.replaceAll("null", "");
        }

        if (newMachineAdress == null || "".equals(newMachineAdress)) {
            newMachineAdress = address;
        }

        return Arrays.asList(newMachineAdress.split("-"));
    }

    /**
     * 获取实际店铺清洗地址，兼容工厂代理或者二次分拣地址
     *
     * @param orderGoodPo
     * @param shopPo
     * @return
     */
    public static MtUserAddress getDeliveryAddress(OrderGoodPo orderGoodPo, ShopPo shopPo) {

        log.info("店铺信息为 {}", JSONObject.toJSON(shopPo));

        MtUserAddress shopAddress = new MtUserAddress();
        // 判断上面名称中是否存在衣服或者裤子获取羽绒服等关键字
        boolean flag = isGoodsName(orderGoodPo.getName());

        // 存在有包含衣鞋混洗的单子需要获取另外的地址
        JSONObject jsonObject = JSON.parseObject(shopPo.getExtendInfo());
        FactoryPo factoryPo = SpringUtils.getBean(FactoryAddressHelper.class)
            .getFactoryAddressWithShopAddress(shopPo.getProvincesName(), shopPo.getCityName(), shopPo.getCountryName(),shopPo.getBrandId());
        // 1、店铺没有开启二次分单 0
        // 2、全是鞋子
        // 3、开启代理工厂清洗 1
        // 4、二次分洗jsonObject为空
        // 以上3个条件中的任何一个为true就获取shopPo.getAddress()地址去清洗
        if (StringUtils.equals(shopPo.getIsSecondSplitOrder(), "0")) {
            /*            shopAddress.setReceiverName(shopPo.getShopName()); TODO 修改为取工厂的地址 和联系方式
            shopAddress.setReceiverMobile(shopPo.getPhone());*/
            shopAddress.setReceiverName(factoryPo.getFactoryManager());
            shopAddress.setReceiverMobile(factoryPo.getFactoryPhone());
            StringBuilder fullAddress = new StringBuilder();
            fullAddress.append(factoryPo.getFactoryAddress());
            /* fullAddress.append(shopPo.getProvincesName());
            fullAddress.append(shopPo.getCityName());TODO 修改为取工厂的地址 和联系方式
            fullAddress.append(shopPo.getCountryName());
            fullAddress.append(shopPo.getAddress());
            fullAddress.append(factoryPo.getFactoryAddress());*/
            shopAddress.setReceiverAddress(fullAddress.toString());
            return shopAddress;
        } else {
            if (StringUtils.equals("1", shopPo.getIsLaundryFactory()) || !flag || Objects.isNull(jsonObject)) {
                /* shopAddress.setReceiverName(shopPo.getShopName());
                shopAddress.setReceiverMobile(shopPo.getPhone());
                StringBuilder fullAddress = new StringBuilder();
                fullAddress.append(shopPo.getProvincesName());
                fullAddress.append(shopPo.getCityName());
                fullAddress.append(shopPo.getCountryName());TODO 修改为取工厂的地址 和联系方式
                fullAddress.append(shopPo.getAddress());*/
                shopAddress.setReceiverName(factoryPo.getFactoryManager());
                shopAddress.setReceiverMobile(factoryPo.getFactoryPhone());
                StringBuilder fullAddress = new StringBuilder();
                fullAddress.append(factoryPo.getFactoryAddress());
                /* fullAddress.append(shopPo.getProvincesName());
                fullAddress.append(shopPo.getCityName());TODO 修改为取工厂的地址 和联系方式
                fullAddress.append(shopPo.getCountryName());
                fullAddress.append(shopPo.getAddress());
                fullAddress.append(factoryPo.getFactoryAddress());*/
                shopAddress.setReceiverAddress(fullAddress.toString());
                return shopAddress;
            }
            // 存在有包含衣鞋混洗的单子需要获取扩展信息中的二次分拣地址
            String receiverAddress = jsonObject.getString("splitCleaningAddress");
            String receiverName = jsonObject.getString("receiverName");
            String receiverMobile = jsonObject.getString("receiverMobile");
            shopAddress.setReceiverAddress(receiverAddress);
            shopAddress.setReceiverMobile(receiverMobile);
            shopAddress.setReceiverName(receiverName);
            return shopAddress;
        }
    }

    /**
     * 判断商品名称中是否存在衣服关键字
     *
     * @param name
     * @return
     */
    public static boolean isGoodsName(String name) {

        return StringUtils.contains(name, "衣") || StringUtils.contains(name, "裤") || StringUtils.contains(name, "羽绒服")
            || StringUtils.contains(name, "沙发") || StringUtils.contains(name, "包") || StringUtils.contains(name, "窗纱")
            || StringUtils.contains(name, "窗帘") || StringUtils.contains(name, "床上") || StringUtils.contains(name, "被子")
            || StringUtils.contains(name, "毛毯");
    }

    public static void main(String[] args) {

        System.out.println(addressCutting("北京市朝阳区幸福小区101"));

        System.out.println(addressCutting("上海市黄浦区鑫浩壹都A座10楼"));

        System.out.println(addressCutting("广东省深圳市罗湖区幸福小区XX区"));

        System.out.println(addressCutting("湖南省长沙市某某县幸福小区"));

        System.out.println(addressCutting("广东省深圳市南山区桃源街道桃源社区高发西路XX号深圳市XX公司X层A区"));

        System.out.println(addressCutting("广东省深圳市南山区北环路第五工业区XX楼三楼A区"));

        System.out.println(addressCutting("天津市和平区和平区南市盒华安大街交口XX有限公司"));

        System.out.println(addressCutting("天津市和平区南市盒华安大街交口XX有限公司"));

        System.out.println(addressCutting("湖北省武汉市洪山区XX街道"));

        System.out.println(addressCutting("湖北省恩施土家族苗族自治州恩施市XX区"));

        System.out.println(addressCutting("内蒙古自治区兴安盟科尔沁右翼前旗XX区"));

        System.out.println(addressCutting("西藏自治区日喀则地区日喀则市XX区"));

        System.out.println(addressCutting("海南省省直辖县级行政单位中沙群岛的岛礁及其海域XX区"));
        List<String> list = addressCutting("四川省成都市双流区 万锦城");
    }

}
